<?php
/**
 * User:chendenan
 * Created On 2022/3/16 0016 21:25
 */
namespace app\common\model\mysql;

class Category extends ModelBase {
    protected $autoWriteTimestamp = true;

    /**
     * 根据分类名获取分类
     * @param $name
     * @return Category|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCategoryByName($name){
        $result = $this->where("name",$name)->find();
        return $result;
    }

    /**
     * 查询分类
     * @param $field
     * @return Category[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCategorys($field="*"){
        $where = [
            "status" => config("status.mysql.table_normal"),
        ];
        $order = [
            "order" => "desc",
            "id" => "desc",
        ];
        $result = $this->where($where)->field($field)->order($order)->select();
        return $result;
    }
    public function getLists($where,$num=10){
        $order = [
//            "order" => "desc",
//            "id" => "desc",
        ];
        $result = $this->where("status","<>",config("status.mysql.table_delete"))->where($where)->order($order)->paginate($num);
        return $result;
    }

    public function getByPid($pid,$field){
        $where = [
            "pid"=>$pid,
            "status"=>config("status.mysql.table_normal"),
        ];
        $order = [
            "order"=>"desc",
            "id"=>"desc",
        ];
        $res = $this->where($where)->field($field)->order($order)->select();
        return $res;
    }
    public function getCategoryById($categoryId,$field){
        $result = $this->field($field)->find($categoryId)->toArray();
        $resultChild = $this->field($field)->where("pid",$categoryId)->select()->toArray();
        if($resultChild){
            $result["list"] = $resultChild;
        }
        return $result;
    }
}